Skip to content

ProxyMapper

Base classes

Name Children Inherits
Object
prettyqt.core.object
The base class of all Qt objects.

â‹” Inheritance diagram

graph TD
  3806349029["itemmodels.ProxyMapper"]
  3251382671["core.Object"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  3251382671 --> 3806349029
  3227531497 --> 3251382671
  2751680898 --> 3227531497
  3984060476 --> 3251382671
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: Object

Class to map indexes / ItemSelections from one proxy to another.

Also handles cases like:

            Root model
                |
           shared proxy
           /                      Proxy_1_1     Proxy_2_1
          |            |
        Proxy_1_2     Proxy_2_2
When mapping from 1_2 to 2_2, it will find the closest parent ("shared proxy" here), use mapToSource / mapSelectionFromSource until it gets there, and then mapFromSource / mapSelectionFromSource to get down to 2_2.
mapper = ProxyMapper(proxy_1_2, proxy_2_1)
index = proxy_1_2.index(0, 0)
mapped_index = mapper.map_index(source=0, target=1, index)

Source code in prettyqt/itemmodels/proxies/proxymapper.py
class ProxyMapper(core.Object):
    """Class to map indexes / ItemSelections from one proxy to another.

    Also handles cases like:
    ```
                Root model
                    |
               shared proxy
               /          \
            Proxy_1_1     Proxy_2_1
              |            |
            Proxy_1_2     Proxy_2_2
    ```
    When mapping from 1_2 to 2_2, it will find the closest parent ("shared proxy" here),
    use mapToSource / mapSelectionFromSource until it gets there,
    and then mapFromSource / mapSelectionFromSource to get down to 2_2.
    ``` py
    mapper = ProxyMapper(proxy_1_2, proxy_2_1)
    index = proxy_1_2.index(0, 0)
    mapped_index = mapper.map_index(source=0, target=1, index)
    ```

    """

    def __init__(
        self,
        *proxies: core.QAbstractItemModel,
        **kwargs,
    ):
        super().__init__(**kwargs)
        chains = [modelhelpers.get_proxy_chain(proxy) for proxy in proxies]
        common_list = [
            element
            for element in chains[0]
            if all(element in sublist for sublist in chains[1:])
        ]
        if not common_list:
            msg = "No common source model"
            raise RuntimeError(msg)
        common_source = common_list[0]
        logger.debug("Common source: %s", common_source)
        # everything before the common source is a proxy
        self._chains = [
            cast("list[core.QAbstractProxyModel]", chain[: chain.index(common_source)])
            for chain in chains
        ]

    def map_index(
        self, source: int, target: int, index: core.ModelIndex
    ) -> core.ModelIndex:
        """Map index from source to target."""
        for model in self._chains[source]:
            logger.debug("mapping from %r to %r", model, model.sourceModel())
            index = model.mapToSource(index)
        for model in reversed(self._chains[target]):
            logger.debug("mapping from %r to %r", model.sourceModel(), model)
            index = model.mapFromSource(index)
        return index

    def map_selection(
        self, source: int, target: int, selection: core.QItemSelection
    ) -> core.QItemSelection:
        """Map selection from source to target."""
        for model in self._chains[source]:
            logger.debug("mapping from %r to %r", model, model.sourceModel())
            selection = model.mapSelectionToSource(selection)
        for model in reversed(self._chains[target]):
            logger.debug("mapping from %r to %r", model.sourceModel(), model)
            selection = model.mapSelectionFromSource(selection)
        return selection

map_index(source: int, target: int, index: core.ModelIndex) -> core.ModelIndex

Map index from source to target.

Source code in prettyqt/itemmodels/proxies/proxymapper.py
def map_index(
    self, source: int, target: int, index: core.ModelIndex
) -> core.ModelIndex:
    """Map index from source to target."""
    for model in self._chains[source]:
        logger.debug("mapping from %r to %r", model, model.sourceModel())
        index = model.mapToSource(index)
    for model in reversed(self._chains[target]):
        logger.debug("mapping from %r to %r", model.sourceModel(), model)
        index = model.mapFromSource(index)
    return index

map_selection(source: int, target: int, selection: core.QItemSelection) -> core.QItemSelection

Map selection from source to target.

Source code in prettyqt/itemmodels/proxies/proxymapper.py
def map_selection(
    self, source: int, target: int, selection: core.QItemSelection
) -> core.QItemSelection:
    """Map selection from source to target."""
    for model in self._chains[source]:
        logger.debug("mapping from %r to %r", model, model.sourceModel())
        selection = model.mapSelectionToSource(selection)
    for model in reversed(self._chains[target]):
        logger.debug("mapping from %r to %r", model.sourceModel(), model)
        selection = model.mapSelectionFromSource(selection)
    return selection

:fa-brands-github: Show source on GitHub